//
//  PPCommonPicturesUploaderManager.m
//  pickpic
//
//  Created by GaoYongyue on 16/9/6.
//  Copyright © 2016年 MicroDream. All rights reserved.
//

#import "PPCommonPicturesUploaderManager.h"
#import "PPUploadPicture.h"
#import "PPImageCompressHelper.h"
#import "UIImage+fixOrientation.h"



@interface PPCommonPicturesUploaderManager ()
@property (nonatomic, strong)QNUploadManager *upManager;
@end

@implementation PPCommonPicturesUploaderManager

SHARED_INSTANCE(PPCommonPicturesUploaderManager);

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        _upManager = [[QNUploadManager alloc] init];
       
        [[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:@"currentUserID" options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
    }
    return self;
}

- (void)uploadPicture:(PPUploadPicture *)picture completion:(CompleteCallback)completion
{
    NSData *imageData = UIImageJPEGRepresentation(picture.image, 1.0f);//[PPImageCompressHelper compressedDataWithImage:picture.image withinSize:CGSizeMake(2000, 2000)];

    NSString *token = picture.token; //从服务端SDK获取
    QNUploadManager *upManager = [[QNUploadManager alloc] init];
    [upManager putData:imageData key:picture.key token:token complete: ^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        !completion ?: completion(key, info.error);
        
        PPLog(@"上传info = %@, resp = %@ - %@",info,resp[@"key"],resp[@"value"]);
        
    } option:nil];

}


- (void)uploadPicture:(PPUploadPicture *)picture completion:(CompleteCallback)completion progress:(QNUpProgressHandler)progress
{
    QNUploadOption *opt = [[QNUploadOption alloc] initWithMime:@"image/jpg"
                                               progressHandler:progress
                                                        params:nil
                                                      checkCrc:NO
                                            cancellationSignal:nil];

    NSData *imageData = UIImageJPEGRepresentation(picture.image, 1.f);//[PPImageCompressHelper compressedDataWithImage:picture.image withinSize:CGSizeMake(2000, 2000)];

    NSString *token = picture.token; //从服务端SDK获取
    QNUploadManager *upManager = [[QNUploadManager alloc] init];

    [upManager putData:imageData key:picture.key token:token complete: ^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        !completion ?: completion(key, info.error);
    } option:opt];
}

#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{

}



@end
